package cn.com.lyj6851.auth.authentication;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.com.lyj6851.common.vo.Result;
import lombok.extern.slf4j.Slf4j;

/**
 * 默认的退出成功处理器，如果设置了yong.security.browser.signOutUrl，则跳到配置的地址上，
 * 如果没配置，则返回json格式的响应。
 */

@Slf4j
public class YongLogoutSuccessHandler implements LogoutSuccessHandler{

	private String signOutSuccessUrl;

    private ObjectMapper objectMapper = new ObjectMapper();
    
	public YongLogoutSuccessHandler(String signOutSuccessUrl) {
		this.signOutSuccessUrl = signOutSuccessUrl;
	}
	
	@Override
	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
			throws IOException, ServletException {
		log.info("退出成功");
		if (StringUtils.isBlank(signOutSuccessUrl)) {
            response.setContentType("application/json;charset=UTF-8");
            response.getWriter().write(objectMapper.writeValueAsString(Result.ok()));
        } else {
            response.sendRedirect(signOutSuccessUrl);
        }
		
	}

}
